package com.neteast.androidclient.contact.widget;


import java.util.List;

import com.neteast.androidclient.contact.R;
import com.neteast.androidclient.contact.Test;
import com.neteast.androidclient.contact.activity.ContactActivity;
import com.neteast.androidclient.contact.activity.NewContactActivity;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


public class ContactPanel extends ViewGroup {
	private String TAG = "AppPanel";
	private Context mContext;
	//x,y轴[x控制列,y控制行]
	public final static int x = 4;
	public final static int y = 3;
	public final static int mMarginTop = 27;// 单屏整块与顶部的距离
	public final static int mMarginLeft = 45;//单屏整块与左边距离
	public final static int mPaddingTop = 12; //每个子块之间的上下距离
	public final static int mPaddingLeft = 11; //每个子块之间的左右距离
	public static int mControlWidth;
	//屏数
	private int mScreen;
	
	
	public ContactPanel(Context context, PackageManager pm, List<ResolveInfo> appList, final RelativeLayout createNewLayout) {
		super(context);
		this.mContext = context;
		//总屏数
		this.mScreen = (int)Math.ceil(((double)appList.size()) / (x * y));
		
		//初始化childview
		for (ResolveInfo info : appList) {
			final CharSequence text = info.loadLabel(pm);
			Drawable d = info.loadIcon(pm);
			View view = LayoutInflater.from(mContext).inflate(R.layout.contact_card_item, null);
			((ImageView)view.findViewById(R.id.imageView)).setImageDrawable(d);
			((TextView)view.findViewById(R.id.name)).setText("杨冬梅");
			((TextView)view.findViewById(R.id.nickName)).setText("Angel");
			((TextView)view.findViewById(R.id.company)).setText("N多市场");
			((TextView)view.findViewById(R.id.position)).setText("策划");
			((TextView)view.findViewById(R.id.phone)).setText("13888888888");
			((TextView)view.findViewById(R.id.email)).setText("yangdm@net-east.com");
			view.setFocusable(true);
			addView(view);
			view.setOnClickListener(new OnClickListener() {
				
				public void onClick(View v) {
					Toast.makeText(mContext, text, Toast.LENGTH_LONG).show();
					mContext.startActivity(new Intent(mContext, NewContactActivity.class));
				}
			});
			
			
		}
	}
	
	//测量
		@Override
		protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
			int widthSize = MeasureSpec.getSize(widthMeasureSpec);
			int heightSize = MeasureSpec.getSize(heightMeasureSpec);
			Log.d(TAG, "widthSize : "+widthMeasureSpec+" heightSize : "+heightMeasureSpec+"---------------");
			mControlWidth = widthSize;
			//设置每个子View的宽高, offX和offY分别表示在横竖方向上需要空出的总距离
			int offX = mMarginLeft * 2 + mPaddingLeft * (x - 1);
			int offY = mMarginTop * 2 + mPaddingTop * (y - 1);
			final int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec((widthSize-offX) / x, MeasureSpec.EXACTLY);
			final int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec((heightSize-offY) / y, MeasureSpec.EXACTLY);
			
			final int count = getChildCount();
			for(int i = 0; i < count; i ++){
				getChildAt(i).measure(childWidthMeasureSpec, childHeightMeasureSpec);
			}
			//设置总屏宽高
			setMeasuredDimension(widthSize * mScreen, heightSize);
		}
		
	@Override
	protected void onLayout(boolean change, int l, int t, int r, int b) {
		if(change){
			final int count = getChildCount();//view数量
			int childLeft = l + mMarginLeft;
			int childTop = t + mMarginTop;
			View view = null;
			int index = 0;
			//屏,行,列
			for (int i = 0; i < mScreen; i++) {
				for (int j = 0; j < y; j++) {
					for (int k = 0; k < x; k++) {
						if(index >= count){
							break;
						}
						view = getChildAt(index);
						view.layout(childLeft, childTop, childLeft + view.getMeasuredWidth(), childTop + view.getMeasuredHeight());
						childLeft += (view.getMeasuredWidth() + mPaddingLeft);//设置每块的左右边距11
						index ++;
					}
					childLeft = mControlWidth * i + mMarginLeft;
					childTop = (view.getMeasuredHeight() + mPaddingTop) * (j + 1) + mMarginTop;//设置每块的上下边距是12
				}
				childLeft = mControlWidth * (i + 1) + mMarginLeft;
				childTop = t + mMarginTop;
			}
		}
	}

	
}
